package com.redrose.rxandroiddemo.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.redrose.rxandroiddemo.R;

public class CustomLayout extends ConstraintLayout {

    private static final String TAG = "CustomLayout";
    private RecyclerView mRecyclerView;

    private Status mStatus = Status.MEDIUM;

    /**
     * 距离顶部的距离
     */
    private int distanceToTop = 0;

    private static final int STATUS_TOP = 0;
    private static final int STATUS_BOTTOM = 2;
    private static final int STATUS_OTHER = 1;
    private int childViewStatus = STATUS_TOP;
    private Scroller mScroller;
    private int mFullHeight;
    private int mediumHeight;
    private int bottomHeight;
    private int mTouchSlop;

    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mScroller = new Scroller(getContext());
        ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
        mTouchSlop = viewConfiguration.getScaledPagingTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
//        int downX = 0;
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                break;
//            case MotionEvent.ACTION_MOVE:
//                float move = ev.getY();
//                if (Math.abs(move - downX)  > mTouchSlop && mStatus == Status.BOTTOM || mStatus == Status.MEDIUM) {
//                    return true;
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mFullHeight = getMeasuredHeight();
        mediumHeight = mFullHeight / 2;
        bottomHeight = (int) getResources().getDimension(R.dimen.dp_56);
        setMediumStatus();
        distanceToTop = mediumHeight;
        Log.d(TAG, "onMeasure: mFullHeight = " + mFullHeight + " mediumHeight = " + mediumHeight);
    }

    float downX = 0;
    float downY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float tempY = event.getY();
                // 起点减去终点
                float distance = downY - tempY;
                distanceToTop = (int) (tempY - downY) + distanceToTop;
                smoothScrollBy(0, (int) distance);
                downY = tempY;
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "onTouchEvent: distanceToTop = " + distanceToTop);
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
        }


        return true;
    }

    public enum Status {
        TOP, MEDIUM, BOTTOM
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (mRecyclerView.canScrollVertically(1)) {
                        Log.i(TAG, "direction 1: true");
                    } else {
                        Log.i(TAG, "direction 1: false");//滑动到底部
                        childViewStatus = STATUS_BOTTOM;
                    }
                    if (mRecyclerView.canScrollVertically(-1)) {
                        Log.i(TAG, "direction -1: true");
                    } else {
                        Log.i(TAG, "direction -1: false");//滑动到顶部
                        childViewStatus = STATUS_TOP;
                    }
                }
            });
        }
    }

    //调用此方法滚动到目标位置
    public void smoothScrollTo(int fx, int fy) {
        int dx = fx - mScroller.getFinalX();
        int dy = fy - mScroller.getFinalY();
        smoothScrollBy(dx, dy);
    }

    private void smoothScrollBy(int dx, int dy) {
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);
        invalidate();
    }

    private void setFullStatus() {
        smoothScrollTo(0, 0);
        mStatus = Status.TOP;
    }

    private void setMediumStatus() {
        smoothScrollTo(0, -(mFullHeight - mediumHeight));
        mStatus = Status.MEDIUM;
    }

    private void setBottomStatus() {
        smoothScrollTo(0, -(mFullHeight - bottomHeight));
        mStatus = Status.BOTTOM;
    }
}